package com.health.ts.service.sysmanage;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.ts.constant.CustomPage;
import com.health.ts.constant.FrontPage;
import com.health.ts.dao.sysmanage.SysDepartmentMapper;
import com.health.ts.dao.sysmanage.SysHospitalMapper;
import com.health.ts.dao.sysmanage.SysUserMapper;
import com.health.ts.entity.sysmanage.SysDepartment;
import com.health.ts.entity.sysmanage.SysHospital;
import com.health.ts.entity.sysmanage.SysUser;
import com.health.ts.service.BaseServiceImpl;
import com.health.ts.utils.AssertUtil;
import com.health.ts.utils.GenerateIdUtil;
import com.health.ts.utils.SessionUserUtil;

/**
 * 医院信息服务
 */
@Service
public class SysHospitalService extends BaseServiceImpl<SysHospitalMapper, SysHospital> {
	
	@Autowired
	SysDepartmentMapper departmentMapper;
	
	@Autowired
	SysUserMapper sysUserMapper;
	
	/**
	 * 查询医院分页列表
	 * @param page
	 * @param param
	 * @return
	 */
	public CustomPage<SysHospital> selectHospitalPage(FrontPage frontPage) {
		QueryWrapper<SysHospital> queryWrapper = new QueryWrapper<SysHospital>();
		//根据医院名称查询
		String hospital_name =getParam(frontPage,"hospital_name");
		if (StringUtils.isNotEmpty(hospital_name)){
			queryWrapper.like("hospital_name", hospital_name); 
		}
		
		//医院管理员只返回自己医院列表
		if(SessionUserUtil.userIsHosAdmin()){
			queryWrapper.eq("id", SessionUserUtil.getUserHosId());
		}
		Page<SysHospital> pagePlus = frontPage.getPagePlus();
		pagePlus.setRecords(this.getBaseMapper().selectHospitalPage(pagePlus, queryWrapper));
		CustomPage<SysHospital> customPage = new CustomPage<SysHospital>(pagePlus);
	    return customPage;
	}
	
	/**
	 * 查询医院列表(不带分页)
	 * @return
	 */
	public List<SysHospital> listHospital() {
		QueryWrapper<SysHospital> QueryWrapper = new QueryWrapper<SysHospital>();
		//医院管理员只返回自己医院列表
		if(SessionUserUtil.userIsHosAdmin()){
			QueryWrapper.eq("id", SessionUserUtil.getUserHosId());
		}
		QueryWrapper.orderByDesc("update_time");
		List<SysHospital> list=this.list(QueryWrapper);
		return list;
	}
	
	/**
	 * 获取单个医院
	 * @param id
	 * @return
	 */
	public SysHospital selectHospital(String hospitalid) {
		SysHospital sysHospital=this.getById(hospitalid);
		AssertUtil.notNull(sysHospital, "根据入参医院id没有查询到医院信息");
		return sysHospital;
	}
	
	/**
	 * 医院信息新增
	 * @param id
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void addHospital(SysHospital hospital) {
		QueryWrapper<SysHospital> QueryWrapper = new QueryWrapper<SysHospital>();
		QueryWrapper.eq("hospital_code", hospital.getHospital_code());
		List<SysHospital> checkList = this.list(QueryWrapper);
		AssertUtil.empty(checkList, "新增失败：医院编码【"+hospital.getHospital_code()+"】已存在");
		Date now=new Date();
		hospital.setId(GenerateIdUtil.makeID());
		hospital.setCreate_time(now);
		hospital.setUpdate_time(now);
		this.save(hospital);
	}
	
	/**
	 * 医院信息修改
	 * @param id
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void updateHospital(String hospitalid, SysHospital hospital) {
		SysHospital info = this.getById(hospitalid);
		AssertUtil.notNull(info, "要修改的医院不存在！");
		hospital.setId(hospitalid);
		hospital.setUpdate_time(new Date());
		this.updateById(hospital);
		
		//更新科室表医院名称
		SysDepartment dept = new SysDepartment();
		dept.setHospital_name(hospital.getHospital_name());
		QueryWrapper<SysDepartment> deptQueryWrapper = new QueryWrapper<SysDepartment>();
		deptQueryWrapper.eq("hospital_id", hospitalid);
		departmentMapper.update(dept, deptQueryWrapper);
		
		//更新用户表医院名称
		SysUser user = new SysUser();
		user.setHospital_name(hospital.getHospital_name());
		QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<SysUser>();
		userQueryWrapper.eq("hospital_id", hospitalid);
		sysUserMapper.update(user, userQueryWrapper);
	}
	
	/**
	 * 医院信息删除
	 * @param id
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void deleteHospital(String hospitalid) {
		//查看医院下是否存在科室
		QueryWrapper<SysDepartment> deptQueryWrapper = new QueryWrapper<SysDepartment>();
		deptQueryWrapper.eq("hospital_id", hospitalid);
		List<SysDepartment> checkList=departmentMapper.selectList(deptQueryWrapper);
		AssertUtil.empty(checkList, "该医院存在下属科室，不可删除");
		this.removeById(hospitalid);
	}
}
